package base;

import model.User;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class InitExcel {
    public static List<User> users = new LinkedList();
    private static Integer inn = Integer.valueOf(0);

    public InitExcel() {
    }

    public static void readXml() {
        File f = new File("resources/data/data.xlsx");
        if (f == null) {
            System.out.println("请放入数据文件");
            System.exit(0);
        } else {
            boolean isE2007 = false;
            if (f.getPath().endsWith("xlsx")) {
                isE2007 = true;
            }

            try {
                InputStream input = new FileInputStream(f.getPath());
                Workbook wb = null;
                if (isE2007) {
                    wb = new XSSFWorkbook(input);
                } else {
                    wb = new HSSFWorkbook(input);
                }
                Sheet sheet =wb.getSheetAt(0);
                Iterator<Row> rows = sheet.rowIterator();
                rows.next();
                while(true) {
                    while(rows.hasNext()) {
                        User user = new User();
                        Row row = rows.next();
                        System.out.println("正在导入第#" + (row.getRowNum()) + "行数据");
                        row.getCell(0).setCellType(CellType.STRING);
                        row.getCell(1).setCellType(CellType.STRING);
                        row.getCell(11).setCellType(CellType.STRING);
                        String name = row.getCell(1).getStringCellValue().trim();
                        String no = row.getCell(0).getStringCellValue();
                        String time = row.getCell(4).getStringCellValue();
                        String mobile = row.getCell(11).getStringCellValue();
                        if (name != null && name != "") {
                            user.setNo(no);
                            user.setName(name);
                            user.setTime(time);
                            user.setMobile(mobile);
                            users.add(user);
                        } else {
                            System.out.println("跳过第#" + (row.getRowNum() - 1) + "行空数据");
                        }
                    }
                    return;
                }
            } catch (IOException var9) {
                var9.printStackTrace();
            }
        }

    }

    public static synchronized User getOneUser() {
        if (users.size() == inn.intValue()) {
            return null;
        } else {
            User user = (User)users.get(inn.intValue());
            Integer var1 = inn;
            inn = inn.intValue() + 1;
            return user;
        }
    }
}
